package org.spring.blog.service.impl;

import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class TaskService {

    final
    JavaMailSenderImpl javaMailSender;

    final
    MailProperties mailProperties;

    public TaskService(JavaMailSenderImpl javaMailSender, MailProperties mailProperties) {
        this.javaMailSender = javaMailSender;
        this.mailProperties = mailProperties;
    }

    /**
     * 发送验证码的异步方法
     *
     * @param _email 邮箱地址
     * @param _code  验证码
     */
    @Async("executor")
    public void sendCodeEmail(String _email, int _code) {
        // send captcha us email
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        simpleMailMessage.setSubject("阳光沙滩博客系统注册验证码");
        simpleMailMessage.setFrom(mailProperties.getUsername());
        simpleMailMessage.setTo(_email);
        simpleMailMessage.setText("你的验证码是：" + _code + "有效期为2分钟，如非本人操作请忽略");

        javaMailSender.send(simpleMailMessage);
    }
}
